import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/components/Layout/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/index.vue'),
      hidden: true
    },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/dashboard/index.vue'),
          meta: { title: '首页', icon: 'House' }
        }
      ]
    },
    {
      path: '/system',
      component: Layout,
      redirect: '/system/user',
      meta: { title: '系统管理', icon: 'Setting' },
      children: [
        {
          path: 'user',
          name: 'User',
          component: () => import('@/views/user/list.vue'),
          meta: { title: '用户管理', icon: 'User' }
        },
        {
          path: 'config',
          name: 'Config',
          component: () => import('@/views/system/config.vue'),
          meta: { title: '系统配置', icon: 'Tools' }
        },
        {
          path: 'profile',
          name: 'Profile',
          component: () => import('@/views/profile/index.vue'),
          meta: { title: '个人中心', icon: 'Avatar' }
        },
        {
          path: 'test-session',
          name: 'TestSession',
          component: () => import('@/views/test-session.vue'),
          meta: { title: 'Session测试', icon: 'Tools' }
        }
      ]
    },
    {
      path: '/content',
      component: Layout,
      redirect: '/content/list',
      meta: { title: '内容管理', icon: 'Document' },
      children: [
        {
          path: 'list',
          name: 'ContentList',
          component: () => import('@/views/content/list.vue'),
          meta: { title: '内容列表', icon: 'List' }
        },
        {
          path: 'add',
          name: 'ContentAdd',
          component: () => import('@/views/content/form.vue'),
          meta: { title: '添加内容', icon: 'Plus' }
        },
        {
          path: 'edit/:id',
          name: 'ContentEdit',
          component: () => import('@/views/content/form.vue'),
          meta: { title: '编辑内容', icon: 'Edit' },
          hidden: true
        },
        {
          path: 'comment',
          name: 'Comment',
          component: () => import('@/views/comment/list.vue'),
          meta: { title: '评论管理', icon: 'ChatDotRound' }
        }
      ]
    },
    {
      path: '/member',
      component: Layout,
      redirect: '/member/plan',
      meta: { title: '会员管理', icon: 'UserFilled' },
      children: [
        {
          path: 'plan',
          name: 'MemberPlan',
          component: () => import('@/views/membership/plans.vue'),
          meta: { title: '套餐管理', icon: 'Postcard' }
        },
        {
          path: 'order',
          name: 'MemberOrder',
          component: () => import('@/views/membership/orders.vue'),
          meta: { title: '订单管理', icon: 'Tickets' }
        }
      ]
    },
    {
      path: '/statistics',
      component: Layout,
      redirect: '/statistics/overview',
      meta: { title: '数据统计', icon: 'DataAnalysis' },
      children: [
        {
          path: 'overview',
          name: 'StatisticsOverview',
          component: () => import('@/views/statistics/overview.vue'),
          meta: { title: '数据概览', icon: 'PieChart' }
        },
        {
          path: 'learning',
          name: 'LearningHistory',
          component: () => import('@/views/statistics/learning.vue'),
          meta: { title: '学习记录', icon: 'Reading' }
        },
        {
          path: 'search',
          name: 'SearchHistory',
          component: () => import('@/views/statistics/search.vue'),
          meta: { title: '搜索统计', icon: 'Search' }
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('admin_token')
  
  if (to.path === '/login') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
